package com.famnote.randomchat;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class ProfileXmlParser {
	
	private static final String ns = null;
	
	/** This is the only function need to be called from outside the class */
	public ArrayList<HashMap<String, String>> parse(Reader reader) 
    		throws XmlPullParserException, IOException{
    	try{    		
    		XmlPullParser parser = Xml.newPullParser();
    		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);    		
    		parser.setInput(reader);
    		parser.nextTag();    		
    		return readProfiles(parser);    		
    	}finally{
    		
    	}
    }        
    
	/** This method read each country in the xml data and add it to List */
    private ArrayList<HashMap<String, String>> readProfiles(XmlPullParser parser) 
    		throws XmlPullParserException,IOException{
    	
    	ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();    	
    	
    	parser.require(XmlPullParser.START_TAG, ns, "profiles");
    	
    	while(parser.next() != XmlPullParser.END_TAG){    		
    		if(parser.getEventType() != XmlPullParser.START_TAG){
    			continue;
    		}
    		
    		String name = parser.getName();    		
    		if(name.equals("profile")){
    			list.add(readProfile(parser));    			
    		}
    		else{
    			skip(parser);
    		}
    	}    	
    	return list;
    }
    
    
    
    
    
    
    
    
    /** This method read a country and returns its corresponding HashMap construct */
    private HashMap<String, String> readProfile(XmlPullParser parser) 
    		throws XmlPullParserException, IOException{
    	
    	parser.require(XmlPullParser.START_TAG, ns, "profile");
    	
        String profileId = parser.getAttributeValue(ns, "id");
        String title="";
        String subtitle="";
        String likes="";
        String photos="";
        String thumb_url="";
        String bg_url="";
        String owner="";
        
        while(parser.next() != XmlPullParser.END_TAG){
        	if(parser.getEventType() != XmlPullParser.START_TAG){
        		continue;
        	}
        	
        	String name = parser.getName();
    		
        	if(name.equals("title")){
        		title = readTitle(parser);
        	}else if(name.equals("subtitle")){
        		subtitle = readSubtitle(parser);        		
        	}else if(name.equals("likes")){
        		likes = readLikes(parser);
        	}else if(name.equals("photos")){
        		photos = readPhotos(parser);
        	}else if(name.equals("thumb_url")){
        		thumb_url = readThumbUrl(parser);
        	}else if(name.equals("bg_url")){
        		bg_url = readBgUrl(parser);
        	}else if(name.equals("owner")){
        		owner = readOwner(parser);
        	}else{
        		skip(parser);
        	}
        }
        
        //String details = 	"Language : " + language + "\n" +
        	//	   			"Capital : " + capital + "\n" + 
        		//   			"Currency : " + currency + "(" + currencyCode + ")";	            			

        
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("id", profileId);
        hm.put("title", title);
		//hm.put("flag", R.drawable.blank);    
		hm.put("subtitle", subtitle);
        hm.put("likes", likes);
        hm.put("photos", photos);
        hm.put("thumb_url", thumb_url);
        hm.put("bg_url", bg_url);
        hm.put("owner", owner);
        
    	return hm;
    }
    
    private String readTitle(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "title");
    	String value = readText(parser);
    	return value;
    }

    private String readSubtitle(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "subtitle");
    	String value = readText(parser);
    	return value;
    }
    
    private String readLikes(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "likes");
    	String value = readText(parser);
    	return value;
    }
    
    private String readPhotos(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "photos");
    	String value = readText(parser);
    	return value;
    }
    
    private String readThumbUrl(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "thumb_url");
    	String value = readText(parser);
    	return value;
    }
    
    private String readBgUrl(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "bg_url");
    	String value = readText(parser);
    	return value;
    }
    
    private String readOwner(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "owner");
    	String value = readText(parser);
    	return value;
    }   
    
    
    
    
    
    
    /** Process language tag in the xml data */    
    private String readLanguage(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "language");
    	String language = readText(parser);
    	return language;
    }
    
    /** Process Capital tag in the xml data */    
    private void readCapital(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "capital");
    	parser.nextTag();
    }
    
    /** Process Currency tag in the xml data */    
    private String readCurrency(XmlPullParser parser) 
    		throws IOException, XmlPullParserException {    
    	parser.require(XmlPullParser.START_TAG, ns, "currency");
    	String currency = readText(parser);
    	return currency;
    }
    
    /** Getting Text from an element */
    private String readText(XmlPullParser parser) 
    		throws IOException, XmlPullParserException{
    	String result = "";
    	if(parser.next()==XmlPullParser.TEXT){
    		result = parser.getText();
    		parser.nextTag();
    	}
    	return result;    	
    }
    
    private void skip(XmlPullParser parser) 
    		throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
     }
}
